<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()" label-width="130px"
             :inline="true">
    <el-form-item label="企业名称" prop="companyName">
      <el-col :span="20">
        <el-input v-model="dataForm.companyName" placeholder="企业名称"></el-input>
      </el-col>
    </el-form-item>
    <el-form-item label="企业性质" prop="companyPropertyId">
      <template>
        <el-col :span="22">
          <el-select clearable v-model="dataForm.companyPropertyId" placeholder="请选择">
            <el-option
              v-for="item in selectOption"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
      </template>
<!--      <el-input v-model="dataForm.companyPropertyId" placeholder="企业性质"></el-input>-->

    </el-form-item>
    <el-form-item label="注册资金" prop="registerMany">
      <el-col :span="20">
        <el-input v-model="dataForm.registerMany" placeholder="注册资金"></el-input>
      </el-col>
    </el-form-item>
    <el-form-item label="企业官网" prop="companyUrl">
      <el-col :span="24">
        <el-input v-model="dataForm.companyUrl" placeholder="企业官网"></el-input>
      </el-col>
    </el-form-item>

    <el-form-item label="所属行业">
      <el-col :span="19">
        <el-cascader
          clearable
          v-model="dataForm.industryOptions"
          :options="industryOptions"
          @change="handleChangeIndustory">
        </el-cascader>
      </el-col>
    </el-form-item>

    <el-form-item label="企业地址" label-width="115px">
      <el-col :span="22">
        <el-cascader
          clearable
          v-model="dataForm.companyAddressOptions"
          :options="companyAddressOptions"
          @change="handleChangeAddress">
        </el-cascader>
      </el-col>
    </el-form-item>

    <el-form-item label="联系人" prop="companyContact">
      <el-col :span="20">
        <el-input v-model="dataForm.companyContact" placeholder="联系人"></el-input>
      </el-col>
    </el-form-item>

    <el-form-item label="联系电话" prop="companyPhone">
      <el-col :span="24">
        <el-input v-model="dataForm.companyPhone" placeholder="联系电话"></el-input>
      </el-col>
    </el-form-item>

    <el-form-item label="联系微信" prop="companyWechat">
      <el-col :span="20">
        <el-input v-model="dataForm.companyWechat" placeholder="联系微信"></el-input>
      </el-col>
    </el-form-item>

    <el-form-item label="联系固话" prop="companyFixPhone">
      <el-col :span="24">
        <el-input v-model="dataForm.companyFixPhone" placeholder="联系固话"></el-input>
      </el-col>
    </el-form-item>

    <el-form-item label="联系邮箱" prop="companyEmail">
      <el-col :span="20">
        <el-input v-model="dataForm.companyEmail" placeholder="联系邮箱"></el-input>
      </el-col>
    </el-form-item>

    <el-form-item label="联系QQ" prop="companyQq">
      <el-col :span="24">
        <el-input v-model="dataForm.companyQq" placeholder="联系QQ"></el-input>
      </el-col>
    </el-form-item>

    <el-form-item label="详细地址" prop="detailPosition">
      <el-col :span="20">
        <el-input v-model="dataForm.detailPosition" placeholder="详细地址"></el-input>
      </el-col>
    </el-form-item>

      <el-form-item label="企业福利" prop="detailPosition">
        <el-col :span="24">
          <el-select
            style="width: 300px"
              v-model="dataForm.companyWalfare"
            clearable
            multiple
            placeholder="请选择">
            <el-option
              v-for="item in walfareOption"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>

    <el-form-item label="企业介绍" prop="companyIntrl">
      <el-col :span="24">
        <el-input
          style="width: 500px"
          type="textarea"
          maxlength="100"
          :rows = "5"
          show-word-limit v-model="dataForm.companyIntrl"
          placeholder="企业介绍">
        </el-input>
      </el-col>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          companyWalfare:[],
          companyName: '',
          registerMany: '',
          companyUrl: '',
          companyContact: '',
          companyPhone: '',
          companyWechat: '',
          companyFixPhone: '',
          companyEmail: '',
          companyQq: '',
          provincesId: '',
          cityesId: '',
          areasId: '',
          tradeId: '',
          classifyId: '',
          jobTypeId: '',
          industryOptions:[],
          companyAddressOptions:[],
          detailPosition: '',
          companyIntrl: '',
          companyPropertyId: '',
        },
        userId:'',                  //当前登录用户的ID



        dataRule: {
          companyName: [
            { required: true, message: '企业名称不能为空', trigger: 'blur' }
          ],
          registerMany: [
            { required: true, message: '注册资金不能为空', trigger: 'blur' }
          ],
          companyUrl: [
            { required: true, message: '企业官网不能为空', trigger: 'blur' }
          ],
          companyContact: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          companyPhone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          companyWechat: [
            { required: true, message: '联系微信不能为空', trigger: 'blur' }
          ],
          companyFixPhone: [
            { required: true, message: '联系固话不能为空', trigger: 'blur' }
          ],
          companyEmail: [
            { required: true, message: '联系邮箱不能为空', trigger: 'blur' }
          ],
          companyQq: [
            { required: true, message: '联系QQ不能为空', trigger: 'blur' }
          ],
          detailPosition: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          companyPropertyId: [
            { required: true, message: '企业性质不能为空', trigger: 'blur' }
          ]
        },
        walfareOption:[],
        selectOption:[],
        industryOptions:[],
        companyAddressOptions:[],
      }
    },
    created () {

    },
    mounted () {
      this.loadingIndustory()
      this.loadingCompanyAddress()
      this.loadingCompanyProperty()
      this.loadingCompanyWalfare()

      this.getUserInfo()

    },
    methods: {

      init (id) {
        this.dataForm.id = id || 0
        this.visible = true

        this.$http({
          url:this.$http.adornUrl('/companys/company/tree-option-need-info/' + id),
          method:'get'
        })
          .then(data => {
            console.log("data",data)
            if (data && data.data.code === 0){
               this.dataForm.companyWalfare = data.data.listDataWelfare
               this.dataForm.industryOptions = data.data.listDataIndustory
               this.dataForm.companyAddressOptions = data.data.listDataAddress
            }
          })


        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/companys/company/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.companyName = data.company.companyName
                this.dataForm.registerMany = data.company.registerMany
                this.dataForm.companyUrl = data.company.companyUrl
                this.dataForm.companyContact = data.company.companyContact
                this.dataForm.companyPhone = data.company.companyPhone
                this.dataForm.companyWechat = data.company.companyWechat
                this.dataForm.companyFixPhone = data.company.companyFixPhone
                this.dataForm.companyEmail = data.company.companyEmail
                this.dataForm.companyQq = data.company.companyQq
                this.dataForm.provincesId = data.company.provincesId
                this.dataForm.cityesId = data.company.cityesId
                this.dataForm.areasId = data.company.areasId
                this.dataForm.detailPosition = data.company.detailPosition
                this.dataForm.companyIntrl = data.company.companyIntrl
                this.dataForm.tradeId = data.company.tradeId
                this.dataForm.classifyId = data.company.classifyId
                this.dataForm.jobTypeId = data.company.jobTypeId
                this.dataForm.companyPropertyId = data.company.companyPropertyId.toString()
              }
            })
          }
        })
      },
      /*加载已经登录的用户*/
      getUserInfo () {
        this.$http({
          url: this.$http.adornUrl('/sys/user/info'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.loading = false
            this.userId = data.user.userId
          }
        })
      },
      //加载行业选项
      loadingIndustory(){
        this.$http({
          url: this.$http.adornUrl('/common/tradecategory/trade-category'),
          //url: this.$http.adornUrl('/workjobs/workjob/tree-option-ccj'),
          method: 'get',
        }).then(({data}) => {
          if (data && data.code === 0){
            this.industryOptions = data.data
          }
        })
      },
      //加载企业地址选项
      loadingCompanyAddress(){
        this.$http({
          url: this.$http.adornUrl('/companys/company/tree-option-pcr'),
          method: 'get',
        }).then(({data}) => {
          if (data && data.code === 0){
            this.companyAddressOptions = data.data
          }
        })
      },
      //加载企业的性质
      loadingCompanyProperty(){
        this.$http({
          url: this.$http.adornUrl('/common/companyproperty/list-option'),
          method: 'get',
        }).then(({data}) => {
          console.log("data",data)
          if (data && data.code === 0){
            this.selectOption = data.data
          }
        })
      },
      //加载企业福利列表
      loadingCompanyWalfare(){
        this.$http({
          url: this.$http.adornUrl('/common/welfare/select-option'),
          method: 'get',
        }).then(({data}) => {
          console.log("data",data)
          if (data && data.code === 0){
            this.walfareOption = data.data
          }
        })
      },

      // 表单提交
      dataFormSubmit () {

        /*表单检查*/
        if (this.dataForm.companyName == ''){
          this.$message({
            type:'warning',
            message:'请输入企业名称'
          })
          return
        }

        if(this.dataForm.registerMany==''){
          this.$message({
            type:'warning',
            message:'请填写注册资金'
          })
          return
        }

        if (this.dataForm.companyPropertyId == ''){
          this.$message({
            type:'warning',
            message:'请选择企业性质'
          })
          return
        }

        if (this.dataForm.companyUrl == ''){
          this.$message({
            type:'warning',
            message:'请填写企业官网'
          })
          return
        }

        if (this.dataForm.companyContact == ''){
          this.$message({
            type:'warning',
            message:'请填写企业联系人'
          })
          return
        }


        if (this.dataForm.companyContact == ''){
          this.$message({
            type:'warning',
            message:'请填写企业联系人'
          })
          return
        }

        if (this.dataForm.companyPhone == ''){
          this.$message({
            type:'warning',
            message:'请填写联系电话'
          })
          return
        }


        if (this.dataForm.companyFixPhone == ''){
          this.$message({
            type:'warning',
            message:'请填写企业联系固话'
          })
          return
        }

        if (this.dataForm.companyWechat == ''){
          this.$message({
            type:'warning',
            message:'请填写联系微信'
          })
          return
        }

        if (this.dataForm.companyQq == ''){
          this.$message({
            type:'warning',
            message:'请填写联系QQ'
          })
          return
        }

        if (this.dataForm.companyEmail == ''){
          this.$message({
            type:'warning',
            message:'请填写企业邮箱'
          })
          return
        }

        if (this.dataForm.detailPosition == ''){
          this.$message({
            type:'warning',
            message:'请填写企业详细地址'
          })
          return
        }


        if (this.dataForm.companyAddressOptions.length == 0){
          this.$message({
            type:'warning',
            message:'请选择企业所在省份和城市'
          })
          return
        }

        if (this.dataForm.industryOptions.length == 0){
          this.$message({
            type:'warning',
            message:'请选择企业所属行业'
          })
          return
        }

        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/companys/company/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'companyWalfare':this.dataForm.companyWalfare,
                'id': this.dataForm.id || undefined,
                'companyName': this.dataForm.companyName,
                'registerMany': this.dataForm.registerMany,
                'companyUrl': this.dataForm.companyUrl,
                'companyContact': this.dataForm.companyContact,
                'companyPhone': this.dataForm.companyPhone,
                'companyWechat': this.dataForm.companyWechat,
                'companyFixPhone': this.dataForm.companyFixPhone,
                'companyEmail': this.dataForm.companyEmail,
                'companyQq': this.dataForm.companyQq,
                'provincesId': this.dataForm.provincesId,
                'cityesId': this.dataForm.cityesId,
                'areasId': this.dataForm.areasId,
                'detailPosition': this.dataForm.detailPosition,
                'companyIntrl': this.dataForm.companyIntrl,
                'tradeId': this.dataForm.tradeId,
                'classifyId': this.dataForm.classifyId,
                'jobTypeId': this.dataForm.jobTypeId,
                'companyPropertyId': this.dataForm.companyPropertyId,
                'createUserId':this.userId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '创建企业成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleChangeIndustory(value){
        console.log("行业的value ===》",value)
        this.dataForm.tradeId = value[0]
        this.dataForm.classifyId = value[1]
        this.dataForm.jobTypeId = value[2]
      },
      handleChangeAddress(value){
        console.log("地址的value ===》",value)
        this.dataForm.provincesId = value[0]
        this.dataForm.cityesId = value[1]
        this.dataForm.areasId = value[2]
      }
    }
  }
</script>
